﻿@using WishlistUtilities;
@{
	PageData["Title"] = "Manage Profile";
	WebSecurity.RequireAuthenticatedUser();

	var db = Database.Open("wishlistdb");
	var currentUser = db.QuerySingle("SELECT * FROM WishlistUser WHERE Id = @0", WebSecurity.CurrentUserId);
}

<h1>Account Management</h1>
@if (IsPost) {
    if (!Request["firstName"].IsEmpty() &&
	    !Request["lastName"].IsEmpty() &&
	    !Request["email"].IsEmpty() && ValidationUtility.IsValidEmail(Request["email"])) {
        //Update user profile
	    try {
		    db.Execute(@"UPDATE WishlistUser SET FirstName = @0, LastName = @1, Email = @2 WHERE Id = @3", 
                Request["firstName"], Request["lastName"], Request["email"], WebSecurity.CurrentUserId);
	    } catch (Exception) {
		    Response.Redirect("~/Shared/Errors/Error");
	    }
        <div class="success">Updated Profile!</div>
        currentUser = db.QuerySingle("SELECT * FROM WishlistUser WHERE Id = @0", WebSecurity.CurrentUserId);
    } else {
	    <div class="error">
            <ul>
                <li>First Name, Last Name, or E-mail cannot be empty.</li>
            </ul>
        </div>
    }
}
<div>
    <p>Hello @currentUser.Username, Please update your account information using the form below:</p>
        <form id="validateForm" method="post" action="">
        <fieldset>
	        <legend>Profile Updates</legend>
	        @if (Roles.IsUserInRole("admin")) {
		        <span class="info">You are an administrator</span>
	        }
	        @RenderPage("~/Shared/_PrePopInputFormField.cshtml",
                new { labelName = "firstName", labelDisplay = "First Name:", inputValue = currentUser.FirstName, maxLength = 50, required = true})
	        @RenderPage("~/Shared/_PrePopInputFormField.cshtml",
                new { labelName = "lastName", labelDisplay = "Last Name:", inputValue = currentUser.LastName, maxLength = 50, required = true})
	        @RenderPage("~/Shared/_PrePopInputFormField.cshtml",
                new { labelName = "email", labelDisplay = "E-mail:", inputValue = currentUser.Email, required = true, 
                    validationClasses = "email", maxLength = 50})
	        <div class="submit-group">
		        <input type="submit" value="Update" onclick="javascript:$('#validateForm').validate()" />
	        </div>
        </fieldset>
    </form>
    Want to change your password? Click <a href="@Href("~/Account/PasswordChange")">here</a>
</div>	